﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ExportToRTR.CApExportToRTRUtils
// Assembly: DnProcessSimulateCommands, Version=16.1.0.400, Culture=neutral, PublicKeyToken=null
// MVID: 135F6F15-CE93-4F75-99F3-E12B8A168841
// Assembly location: C:\Program Files\Tecnomatix_16.1\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Tecnomatix.Engineering;

namespace DnProcessSimulateCommands.ExportToRTR
{
    internal static class CApExportToRTRUtils
    {
        private static readonly string REGISTRY_PATH = TxApplication.RegistryPath + "\\TUNE\\NewAssembler\\Commands Info\\DnProcessSimulateCommands\\Dialogs\\CUiExportToRTRWindow";
        private const char INVALID_CHAR_SUB = '_';
        private static Dictionary<string, int> m_names = new Dictionary<string, int>();

        internal static void SaveValueToRegistry(string key, string value)
        {
            RegistryKey registryKey;
            using (registryKey = Registry.CurrentUser.OpenSubKey(CApExportToRTRUtils.REGISTRY_PATH, true))
            {
                if (registryKey == null)
                    registryKey = Registry.CurrentUser.CreateSubKey(CApExportToRTRUtils.REGISTRY_PATH);
                registryKey.SetValue(key, (object)value);
            }
        }

        internal static string ReadValueFromRegistry(string key)
        {
            string str = (string)null;
            RegistryKey registryKey1 = Registry.CurrentUser.OpenSubKey(CApExportToRTRUtils.REGISTRY_PATH);
            if (registryKey1 != null)
            {
                str = registryKey1.GetValue(key) as string;
            }
            else
            {
                RegistryKey registryKey2 = Registry.LocalMachine.OpenSubKey(CApExportToRTRUtils.REGISTRY_PATH, true);
                if (registryKey2 != null)
                    str = registryKey2.GetValue(key) as string;
            }
            return str ?? string.Empty;
        }

        public static string GetUniqueName(string name)
        {
            string uniqueName = name;
            if (CApExportToRTRUtils.m_names.ContainsKey(name))
            {
                CApExportToRTRUtils.m_names[name]++;
                uniqueName = string.Format("{0}_{1}", (object)name, (object)CApExportToRTRUtils.m_names[name]);
            }
            else
                CApExportToRTRUtils.m_names.Add(name, 0);
            return uniqueName;
        }

        public static void ResetNames() => CApExportToRTRUtils.m_names.Clear();

        public static bool IsVisible(ITxDisplayableObject obj)
        {
            bool flag = false;
            if (obj != null)
                flag = obj.Visibility == TxDisplayableObjectVisibility.All || obj.Visibility == TxDisplayableObjectVisibility.Partial;
            return flag;
        }

        public static bool HasAssignedRobot(ITxRoboticLocationOperation location)
        {
            bool flag = false;
            if (location != null)
                flag = location.ParentRoboticOperation?.Robot != null;
            return flag;
        }

        public static string DoubleArrayToString(this IEnumerable<double> arr)
        {
            return arr.Aggregate<double, string>("", (Func<string, double, string>)((current, num) => current + " " + (object)num)).Substring(1);
        }

        public static string GetValidFileName(string name)
        {
            string validFileName = name;
            foreach (char invalidFileNameChar in Path.GetInvalidFileNameChars())
                validFileName = validFileName.Replace(invalidFileNameChar, '_');
            return validFileName;
        }
    }
}
